package com.gviiii.pprf.netty;

import com.gviiii.pprf.utils.SpringContextUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import static io.netty.channel.ChannelHandler.Sharable;

/**
 * 处理来自服务器的消息
 */
@Sharable
public class RfChannelHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(RfChannelHandler.class);

    private final Map<String, Integer> msgTags = new ConcurrentHashMap<>();

    public static final String MSG_TAG = "MSG_TAG";

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        RfNioClient.channel=ctx.channel();
        logger.info("channel active");
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        MsgTagCacheService msgTagCacheService= SpringContextUtils.getBean("msgTagCacheService");
        if (Objects.isNull(msgTagCacheService)){
            return;
        }
        if (msg instanceof NettyMessage) {
            NettyMessage message = (NettyMessage) msg;
//            logger.info(JsonUtils.witeStringByObject(message));
            switch (message.getHeader()) {
                case Header.REMOVE_MSGTAG:msgTagCacheService.removeMsgTag();break;
                case Header.DISCONNECTED_MSG:ctx.fireChannelInactive();
                    break;
                case Header.CONNECTED_MSG:
//                    msgTags.put(MSG_TAG, (Integer) message.getBody());
                    msgTagCacheService.setMsgTag((Integer) message.getBody());
                    RfNioClient.send(message);
                    break;
                case Header.RECEIVE_PERSONCARD_MSG:
                    setMsgTag(message,msgTagCacheService);
                    break;
                case Header.RECEIVE_POSITIONCARD_MSG:
                    setMsgTag(message,msgTagCacheService);
                    break;
                case Header.RECEIVE_POSITIONCARDREUSE_MSG:
                    setMsgTag(message,msgTagCacheService);
                    break;
                default:
                    break;
            }
//            ReferenceCountUtil.release(msg);
        }
        super.channelRead(ctx, msg);
    }

    private void setMsgTag(NettyMessage message,MsgTagCacheService msgTagCacheService) {
        msgTagCacheService.setMsgTag((Integer) message.getBody());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.info("channel Inactive");
        if (Objects.nonNull(ctx.channel())){
            ctx.channel().close();
        }
        super.channelInactive(ctx);
    }

    public Map<String, Integer> getMsgTags() {
        return msgTags;
    }
}
